package com.ailanyin.admin.service.impl;

import com.ailanyin.mapper.SysErrorLogMapper;
import com.ailanyin.model.SysErrorLog;
import com.ailanyin.admin.service.SysErrorLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2022-01-04 04:41:49
 */
@Service
public class SysErrorLogServiceImpl implements SysErrorLogService {

    @Resource
    private SysErrorLogMapper sysErrorLogMapper;

    @Override
    public List<SysErrorLog> getListByParam(SysErrorLog sysErrorLog) {
        return sysErrorLogMapper.selectList(sysErrorLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSysErrorLog(Exception exception) {
        SysErrorLog sysErrorLog = new SysErrorLog();
        sysErrorLog.setExceptionReason(exception.getMessage());

        StringBuilder builder = new StringBuilder();
        if (exception.getStackTrace().length > 0) {
            for (StackTraceElement traceElement : exception.getStackTrace()) {
                builder.append(traceElement).append("<br>");
            }
        }
        sysErrorLog.setDetailInfo(builder.toString());
        sysErrorLog.setCreateTime(new Date());
        sysErrorLogMapper.insertSelective(sysErrorLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysErrorLog(SysErrorLog sysErrorLog) {
        sysErrorLogMapper.updateByPrimaryKeySelective(sysErrorLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<String> ids) {
        sysErrorLogMapper.deleteByIds(ids);
    }

    @Override
    public SysErrorLog getInfo(Long id) {
        return sysErrorLogMapper.selectByPrimaryKey(id);
    }

}
